from flask import Blueprint

from controllers import R
from mauth import HasPerm
from models.vis_log import VisLog
from services.vis_log_service import VisLogService
from validators import BasePageForm
from validators.id_validator import IdForm, IdsForm
from validators.vis_log_validator import VisLogForm

# 声明一个蓝图
vis_log = Blueprint("vis_log", __name__, url_prefix="/sys/visLog")

# 声明一个系统访问日志表业务服务
vis_log_service = VisLogService(model=VisLog)


@vis_log.route("/detail", methods=["POST"])
@HasPerm(access="sys:visLog:detail", name="通过id获取系统访问日志表信息")
def vis_log_detail():
    """
    通过id获取系统访问日志表信息
    :return:
    """
    form = IdForm()
    form.validate_for_api()
    # 可通过form.data获取所有提交参数
    # 或者直接拿id值 id=form.id.data
    # u = VisLog.query.filter_by(id=form.id.data).first()
    # 通过主键查询
    u = vis_log_service.detail(form)
    if u is not None:
        return R.data(u.to_dict())
    else:
        return R.fail("该记录不存在")


@vis_log.route("/page", methods=["POST"])
@HasPerm(access="sys:visLog:page", name="分页查询系统访问日志表列表")
def vis_log_page():
    """
    分页查询系统访问日志表列表
    :return:
    """
    form = BasePageForm()
    form.validate_for_api()
    return R.data(vis_log_service.page(form))


@vis_log.route("/save", methods=["POST"])
@HasPerm(access="sys:visLog:save", name="添加系统访问日志表")
def vis_log_save():
    """
    添加系统访问日志表
    :return:
    """
    form = VisLogForm()
    form.validate_for_api()
    # 可通过form.data获取所有提交参数
    # print(form.data)
    vis_log_service.save(form)
    return R.success("添加系统访问日志表成功")


@vis_log.route("/update", methods=["POST"])
@HasPerm(access="sys:visLog:update", name="修改系统访问日志表")
def vis_log_update():
    """
    修改系统访问日志表
    :return:
    """
    form = VisLogForm()
    form.validate_for_api()
    # 可通过form.data获取所有提交参数
    # print(form.data)
    vis_log_service.update(form)
    return R.success("修改系统访问日志表成功")


@vis_log.route("/remove", methods=["POST"])
@HasPerm(access="sys:visLog:remove", name="删除系统访问日志表")
def vis_log_remove():
    """
    删除系统访问日志表
    :return:
    """
    form = IdsForm()
    form.validate_for_api()
    # 可通过form.data获取所有提交参数
    # print(form.data)
    vis_log_service.remove(form)
    return R.success("删除系统访问日志表成功")
